﻿/*
 * Copyright (c) HiSilicon (Shanghai) Technologies Co., Ltd. 2023-2023. All rights reserved.
 */

#ifndef PORTBASE_H
#define PORTBASE_H

#include <QObject>
#include <QSerialPort>
#include "BurnToolCommon/StdAfx.h"
#include "BurnToolCommon/MyThread.h"
#include "BurnToolCommon/Global.h"

namespace portbaseConstantValue {
    const unsigned int WRITE_THREAD_ID = 1090;
    const unsigned int WRITE_BUFFER_SIZE = 10 * 0x0400; // 10K
    const unsigned int PORT_LENGTH = 50;
    /* A character was received and placed in the input buffer. */
    constexpr unsigned int WM_COMM_RXCHAR = (WM_USER + 7);
     /* A character was received and placed in the input buffer. */
    constexpr unsigned int WM_COMM_RXCHAR_WITE_THREAD = (WM_USER + 13);

    struct PortBaseInitStru {
        QWidget *pPortOwner;
        UINT portnr;
        UINT baud;
        QSerialPort::Parity parity;
        UINT databits;
        UINT stopbits;
        UINT flowctrl;
        DWORD dwCommEvents;
        UINT writebuffersize;
        UINT witeThreadMsgId;
        MyThreadClass::MyThread *witeThread;
        DWORD oneTimeReadByte;
        BOOL isForceReadWithSleep;
        UINT forceReadTime;
        QString ipAddr;
        UINT ipPort;
    };

}
using namespace portbaseConstantValue;

class PortBase : public QObject {
    Q_OBJECT
public:
    virtual void ClosePort();
    explicit PortBase(QObject *parent = nullptr);
    ~PortBase() override;
    virtual bool InitPort(PortBaseInitStru &portFactionInit);
    virtual ErrorSettingCom InitPortConfigure(UINT stopbits, UINT flowctrl, UINT baud,
                                              QSerialPort::Parity parity, UINT databits);
    virtual BOOL ReInitPort();
    virtual BOOL StartMonitoring();
    virtual BOOL RestartMonitoring();
    virtual BOOL StopMonitoring();

    virtual void WriteToPort(LPCTSTR string);
    virtual void WriteToPort(const char *string, int n);

    virtual bool IsThreadAlive() const;

    virtual void SetOneTimeReadByte(DWORD oneTimeReadByte);
    virtual bool SetFlowCtrl(UINT flowctrl);
    virtual bool SetStopBits(UINT stopBits);
    virtual bool SetDataBits(UINT dataBits);
signals:
    void SignalSendMsg(long int msgId, WPARAM wParam, LPARAM lParam);
};


#endif // PORTBASE_H
